import { createStore } from 'vuex'
import { getEnvId, getModuleId, getProductId, getProjectId,  setProductId, setEnvId, setProjectId,setModuleId } from '@/util/devcloudUtils'

interface IState{
  loadding:boolean,
  flush:string,
  currInfo:ICurrInfo
}

export interface ICurrInfo{
  productId?: number,
  envId: number,
  projectId?: number,
  moduleId?: number,
}

export default createStore({
  state: {
    loadding: false,
    flush: '',
    currInfo: {
      productId: getProductId(),
      envId: getEnvId(),
      projectId: getProjectId(),
      moduleId: getModuleId()
    }
  },
  getters: {
    isloading:(state:any):boolean=>{
      return state.loadding
    },
    isFlush:(state):string=>{
      return state.flush
    },
    getCurrInfo:(state):ICurrInfo=>{
      return state.currInfo
    }
  },
  mutations: {
    showloadding(state:any, load:boolean) {
      state.loadding = load
    },
    toFlush(state, flush:string) {
      state.flush = flush
    },
    setCurrInfo(state, info:ICurrInfo) {
      if(!info.productId){
        state.currInfo.projectId = undefined
        setProductId(undefined)
      }else if(info.productId){
        state.currInfo.productId = info.productId
        setProductId(info.productId)
      }
      if(info.envId){
        state.currInfo.envId = info.envId
        setEnvId(info.envId)
      }
      if(!info.projectId){
        state.currInfo.projectId = undefined
        setProjectId(undefined)
      }else if(info.projectId){
        state.currInfo.projectId = info.projectId
        setProjectId(info.projectId)
      }
      if(!info.moduleId){
        state.currInfo.moduleId = undefined
        setModuleId(undefined)
      }else if(info.moduleId){
        state.currInfo.moduleId = info.moduleId
        setModuleId(info.moduleId)
      }
    }
  },
  actions: {
    showloadding(context:any,load:boolean){
      context.commit("showloadding",load);
    },
    toFlush(context,flush){
      context.commit("toFlush",flush);
    },
    setCurrInfo(context, info:ICurrInfo) {
      context.commit("setCurrInfo", info);
    }
  },
  modules: {
  }
})
